#!/bin/bash
#
# Reset default charset for GNUsed problems...
export LC_ALL=C

# Check that the caller is make
caller=$(ps -p $PPID -o comm=)
if [[ "$caller" != "make" ]]; then
    echo "Error: don't call the '$0' script directly !" >&2
    echo "Use the 'make iso' command instead" >&2
#    exit 1
fi

set -u

# Go to the script directory to create the package
cd "$(dirname $0)"

THEME=metal

SRCROOT="$PWD"
SYMROOT=${SRCROOT}/sym
CLOVERROOT=${SRCROOT}/CloverV2
PKG_BUILD_DIR=${SYMROOT}/package
IMGROOT=${SYMROOT}/CloverCD
IMGSKELROOT=${SRCROOT}/imgskel
CDBOOT=${IMGROOT}/usr/standalone/i386/cdboot
CDBOOT32=${IMGROOT}/usr/standalone/i386/cdboot32
EFIROOT=${SRCROOT}/CloverV2/EFI

REVISION=$(cat ../vers.txt)
PRODUCT=Clover-v2.4k-${REVISION}
CDLABEL=${PRODUCT}-X64
CDLABEL32=${PRODUCT}-IA32
ISOIMAGE=${SYMROOT}/${CDLABEL}.iso
ISOIMAGE32=${SYMROOT}/${CDLABEL32}.iso

IMAGE_WITH_IA32=0
[[ -f ${CLOVERROOT}/Bootloaders/ia32/boot3 ]] && IMAGE_WITH_IA32=1

rm -rf sym/CloverCD
rm -rf sym/CloverISO

# Create a new sym directory
[[ ! -d sym ]] && mkdir sym

echo "====================== Creating cdboot ====================="
mkdir -p ${SYMROOT}/i386
cp -f ${CLOVERROOT}/BootSectors/cdboot ${SYMROOT}/i386/
dd if=${CLOVERROOT}/Bootloaders/x64/boot6 of=${SYMROOT}/i386/cdboot conv=sync bs=2k seek=1 &> /dev/null

echo Update cdboot with boot file size info
declare -r SYSNAME="$(uname)"
if [[ "$SYSNAME" == Linux ]]; then
declare -r STAT_PARAM='-c%s'
else
declare -r STAT_PARAM='-f%z'
fi
stat "$STAT_PARAM" ${CLOVERROOT}/Bootloaders/x64/boot6 \
 | perl -ane "print pack('V',@F[0]);" \
 | dd of=${SYMROOT}/i386/cdboot bs=1 count=4 seek=2044 conv=notrunc &> /dev/null


if [[ $IMAGE_WITH_IA32 -eq 1 ]];then
    echo "===================== Creating cdboot32 ===================="
    mkdir -p ${SYMROOT}/i386
    cp -f ${CLOVERROOT}/BootSectors/cdboot ${SYMROOT}/i386/cdboot32
    dd if=${CLOVERROOT}/Bootloaders/ia32/boot3 of=${SYMROOT}/i386/cdboot32 conv=sync bs=2k seek=1 &> /dev/null

    echo "Update cdboot32 with boot file size info"
    stat "$STAT_PARAM" ${CLOVERROOT}/Bootloaders/ia32/boot3 \
     | perl -ane "print pack('V',@F[0]);" \
     | dd of=${SYMROOT}/i386/cdboot32 bs=1 count=4 seek=2044 conv=notrunc &> /dev/null
fi


echo "======================= Creating ISO ======================="
echo "[RM] ${IMGROOT}"
rm -rf ${IMGROOT}
echo "[MKDIR] ${IMGROOT}/usr/standalone/i386"
mkdir -p ${IMGROOT}/usr/standalone/i386
mkdir -p ${IMGROOT}/usr/standalone/i386/x64
mkdir -p ${IMGROOT}/usr/standalone/i386/ia32
echo "[MKDIR] ${IMGROOT}/EFI/BOOT"
mkdir -p ${IMGROOT}/EFI/BOOT
echo "[MKDIR] ${IMGROOT}/EFI/CLOVER/themes/"
mkdir -p ${IMGROOT}/EFI/CLOVER/themes/
echo "[MKDIR] ${IMGROOT}/usr/bin"
mkdir -p ${IMGROOT}/usr/bin
echo "[MKDIR] ${IMGROOT}/usr/local/bin"
mkdir -p "${IMGROOT}"/usr/local/bin
cp -p "${SYMROOT}"/utils/* "${IMGROOT}"/usr/local/bin
echo "[MKDIR] ${IMGROOT}/Library/Application Support/Clover"
mkdir -p "${IMGROOT}/Library/Application Support/Clover"
cp -p "${SRCROOT}"/CloverUpdater/CloverUpdaterUtility "${IMGROOT}/Library/Application Support/Clover/"
cp -pr "${SRCROOT}"/CloverUpdater/build/CloverUpdater.app "${IMGROOT}/Library/Application Support/Clover/"
echo "[MKDIR] ${IMGROOT}/Library/PreferencePanes"
mkdir -p "${IMGROOT}/Library/PreferencePanes"
cp -pr "${SRCROOT}"/CloverPrefpane/Build/Clover.prefPane "${IMGROOT}/Library/PreferencePanes/"
echo "[CP] cdboot"
cp -f ${SYMROOT}/i386/cdboot ${CDBOOT}
if [[ ${IMAGE_WITH_IA32} -eq 1 ]]; then
    echo "[CP] cdboot32"
    cp -f ${SYMROOT}/i386/cdboot32 ${CDBOOT32}
fi
echo "[CP] metal"
cp -r -f ${CLOVERROOT}/themespkg/metal ${IMGROOT}/EFI/CLOVER/themes/
echo "[CP] BOOTX64"
cp -f ${CLOVERROOT}/EFI/BOOT/BOOTX64.efi ${IMGROOT}/EFI/BOOT/
echo "[CP] CLOVERX64"
cp -f ${CLOVERROOT}/EFI/CLOVER/CLOVERX64.efi ${IMGROOT}/EFI/CLOVER/
if [ ${IMAGE_WITH_IA32} -eq 1 ]; then
    echo "[CP] BOOTIA32"
    cp -f ${CLOVERROOT}/EFI/BOOT/BOOTIA32.efi ${IMGROOT}/EFI/BOOT/
    echo "[CP] CLOVERIA32"
    cp -f ${CLOVERROOT}/EFI/CLOVER/CLOVERIA32.efi ${IMGROOT}/EFI/CLOVER/
fi
# echo "CP font"
# cp -r -f ${CLOVERROOT}/EFI/BOOT/font ${IMGROOT}/EFI/BOOT/
# echo "CP config.plist"
cp -f ${CLOVERROOT}/EFI/CLOVER/config-sample.plist ${IMGROOT}/EFI/CLOVER/config.plist
echo "[CP] drivers64"
cp -r -f ${CLOVERROOT}/EFI/CLOVER/drivers64 ${IMGROOT}/EFI/CLOVER/
cp -r -f ${CLOVERROOT}/EFI/CLOVER/drivers64UEFI ${IMGROOT}/EFI/CLOVER/
echo "[CP] kexts and OEM"
cp -r -f ${CLOVERROOT}/ThirdParty/* ${IMGROOT}/EFI/CLOVER/
echo "[CP] tools"
cp -r -f ${CLOVERROOT}/EFI/CLOVER/tools ${IMGROOT}/EFI/CLOVER/
echo "[CP] doc"
cp -r -f ${CLOVERROOT}/EFI/CLOVER/doc ${IMGROOT}/EFI/CLOVER/
if [ ${IMAGE_WITH_IA32} -eq 1 ]; then
    echo "[CP] drivers32"
    cp -r -f ${CLOVERROOT}/EFI/CLOVER/drivers32 ${IMGROOT}/EFI/CLOVER/
    cp -f ${CLOVERROOT}/drivers-Off/drivers32/UsbMouseDxe-32.efi ${IMGROOT}/EFI/CLOVER/drivers32/
    cp -f ${CLOVERROOT}/drivers-Off/drivers32/Ps2MouseDxe-32.efi ${IMGROOT}/EFI/CLOVER/drivers32/
fi
echo "[CP] drivers-off"
cp -f ${CLOVERROOT}/drivers-Off/drivers64/UsbMouseDxe-64.efi ${IMGROOT}/EFI/CLOVER/drivers64/
cp -f ${CLOVERROOT}/drivers-Off/drivers64/Ps2MouseDxe-64.efi ${IMGROOT}/EFI/CLOVER/drivers64/
if [ ${IMAGE_WITH_IA32} -eq 1 ]; then
    rsync -r ${CLOVERROOT}/drivers-Off ${IMGROOT}/EFI/CLOVER/
else
    rsync -r ${CLOVERROOT}/drivers-Off --exclude='drivers32*' ${IMGROOT}/EFI/CLOVER/
fi
echo "[CP] boot"
cp -f ${CLOVERROOT}/Bootloaders/x64/boot? ${IMGROOT}/usr/standalone/i386/x64/
[ ${IMAGE_WITH_IA32} -eq 0 ] || cp -f ${CLOVERROOT}/Bootloaders/ia32/boot? ${IMGROOT}/usr/standalone/i386/ia32/
cp -f ${CLOVERROOT}/BootSectors/boot0af  ${IMGROOT}/usr/standalone/i386
cp -f ${CLOVERROOT}/BootSectors/boot0ss  ${IMGROOT}/usr/standalone/i386
cp -f ${CLOVERROOT}/BootSectors/boot0md  ${IMGROOT}/usr/standalone/i386
cp -f ${CLOVERROOT}/BootSectors/boot1h   ${IMGROOT}/usr/standalone/i386
cp -f ${CLOVERROOT}/BootSectors/boot1h2  ${IMGROOT}/usr/standalone/i386
cp -f ${CLOVERROOT}/BootSectors/boot1f32 ${IMGROOT}/usr/standalone/i386
cp -f ${CLOVERROOT}/BootSectors/boot1f32alt ${IMGROOT}/usr/standalone/i386
cp -f ${CLOVERROOT}/BootSectors/boot1x    ${IMGROOT}/usr/standalone/i386
cp -f ${CLOVERROOT}/BootSectors/boot1xalt ${IMGROOT}/usr/standalone/i386
# #ifdef CONFIG_FDISK440
# 	cp -f ${CLOVERROOT}/BootSectors/fdisk440 ${IMGROOT}/usr/bin
# #endif
rm -rf ${IMGROOT}/.s*
rm -rf ${IMGROOT}/*/.s*
rm -rf ${IMGROOT}/*/*/.s*
rm -rf ${IMGROOT}/*/*/*/.s*
rm -rf ${IMGROOT}/*/*/*/*/.s*
echo "[HDIUTIL] ${ISOIMAGE}"
mkdir -p ${SYMROOT}/CloverISO-${REVISION}
if [[ "$SYSNAME" == Linux ]]; then
genisoimage -J -hfs -hfs-volid ${CDLABEL} -D -b usr/standalone/i386/cdboot -no-emul-boot -quiet -o "${ISOIMAGE}" ${IMGROOT}
else
hdiutil makehybrid -iso -joliet -hfs -hfs-volume-name ${CDLABEL}  \
 -eltorito-boot ${CDBOOT} -no-emul-boot -ov -o "${ISOIMAGE}"      \
 ${IMGROOT} -quiet
fi
mv -f ${ISOIMAGE} ${SYMROOT}/CloverISO-${REVISION}/
if [ ${IMAGE_WITH_IA32} -eq 1 ]; then
    echo "[HDIUTIL] ${ISOIMAGE32}"
    if [[ "$SYSNAME" == Linux ]]; then
    genisoimage -J -hfs -hfs-volid ${CDLABEL32} -D -b usr/standalone/i386/cdboot32 -no-emul-boot -quiet -o "${ISOIMAGE32}" ${IMGROOT}
    else
    hdiutil makehybrid -iso -joliet -hfs -hfs-volume-name ${CDLABEL32}  \
     -eltorito-boot ${CDBOOT32} -no-emul-boot -ov -o "${ISOIMAGE32}"    \
     ${IMGROOT} -quiet
    fi
    mv -f ${ISOIMAGE32} ${SYMROOT}/CloverISO-${REVISION}/
fi
echo "[TAR] CloverISO-${REVISION}"
tar -cv -f ${SYMROOT}/CloverISO-${REVISION}.tar -C ${SYMROOT}/CloverISO-${REVISION} .
echo "[LZMA] CloverISO-${REVISION}"
if [[ "$SYSNAME" == Linux ]]; then
xz -f -S .lzma ${SYMROOT}/CloverISO-${REVISION}.tar
else
${CLOVERROOT}/BootSectors/lzma e ${SYMROOT}/CloverISO-${REVISION}.tar ${SYMROOT}/CloverISO-${REVISION}.tar.lzma
fi
rm -rf ${SYMROOT}/CloverISO-${REVISION}.tar

# Open the final folder.
if [[ "${1:-}" == "" ]]; then
    ls -la sym
    [[ "$SYSNAME" != Linux ]] && open sym
fi

exit 0
